package top.chenfu.blog.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.chenfu.blog.beans.ResponseData;
import top.chenfu.blog.entity.Category;
import top.chenfu.blog.service.CategoryService;

import java.util.List;

/**
 * @Auther chenfu
 * @2019/10/31 20:36
 * @Desc
 */
@RestController
@RequestMapping("/admin/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/all")
    public List<Category> getAllCategories() {
        return categoryService.getAllCategories();
    }

    /**
     * 增加新的分类
     *
     * @param category
     * @return
     */
    @PostMapping
    public ResponseData addNewCate(Category category) {
        if (StringUtils.isBlank(category.getCateName())) {
            return ResponseData.defaultFail("请输入栏目名称！");
        }
        if (categoryService.addCategory(category) > 0) {
            return ResponseData.defaultSuccess("添加成功！");
        }
        return ResponseData.defaultFail("添加失败！");
    }

    /**
     * 修改分类
     *
     * @param category
     * @return
     */
    @PutMapping
    public ResponseData updateCate(Category category) {
        if (categoryService.updateCategory(category) > 0) {
            return ResponseData.defaultSuccess("修改成功！");
        }
        return ResponseData.defaultFail("修改失败！");
    }

    /**
     * 删除分类
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public ResponseData deleteById(@PathVariable String ids) {
        boolean ok = categoryService.deleteCategoryByIds(ids);
        if (ok) {
            return ResponseData.defaultSuccess("删除成功！");
        }
        return ResponseData.defaultFail("删除失败！");
    }

}
